
#include "shopcar.h"

ShopCar::ShopCar()
{
}
ShopCar::~ShopCar()
{
}
add(int id, int quantity) // 添加物品到购物车
{
    if(id < 0 || id >= MaxCapacity || quantity <= 0)
    {

        cout << "Invalid input!" << endl;
        return;
    }
    if(currentDrink[id].quantity < shopcar[id] + quantity)
    {
        shopcar[id] = currentDrink[id].quantity;
        cout << "没有足够的库存!\n已为您添加所有的库存到购物车!" << endl;
        return;
    }
    shopcar[id] += quantity;
}

remove(int id, int quantity)// 从购物车中移出物品
{
    shopcar[id] -= quantity;
    if (shopcar[id] < 0)
    {
        map.erase(id); // 从购物车中移除该物品
    }
} 

double ShopCar::Checkout() // 结账功能
{
    double sum = 0;
    map<int int>::iterator it = shopcar.begin();
    // 遍历购物车，下标找到饮料，进而得到价格再乘以数量
    for (it : shopcar)
    {
        sum += drinks[it->first].price * it->second; // 计算总价
        drinks[it->first].num -= it->second;         // 更新饮料库存
    }
    shoopcar.clear(); // 清空购物车
    return sum;       // 返回总价
}